


//
//  CustomViewController.m
//  NeiHanDemo
//
//  Created by qianfeng on 16/2/19.
//  Copyright © 2016年 王卓越. All rights reserved.
//

#import "CustomViewController.h"
#import "TabBarModel.h"

@interface CustomViewController ()

@property (nonatomic) NSMutableArray *tabBarModelArray;

@end

@implementation CustomViewController

- (id)init{
    if (self = [super init]) {
        //1、定制 TabBarModels
        [self customTabBarModels];
        //2、创建 ContentViewControllers
        //注意 
        [self createContentViewControllers];
    }
    return self;
}

//1、定制 TabBarModels
- (void)customTabBarModels{
    //注意 一定要先初始化 tabBarModelArray
    self.tabBarModelArray = [NSMutableArray array];
    //创建 TabBarModel
    TabBarModel *jokeModel = [[TabBarModel alloc] initWithTitle:@"段子" normalImage:@"圣斗士@2x" highLightImage:@"圣斗士_selected@2x" vcClass:@"JokeViewController"];
    TabBarModel *picModel = [[TabBarModel alloc] initWithTitle:@"趣图" normalImage:@"海贼王@2x" highLightImage:@"海贼王_selected@2x" vcClass:@"PictureViewController"];
    TabBarModel *videoModel = [[TabBarModel alloc] initWithTitle:@"视频" normalImage:@"火影忍者@2x" highLightImage:@"火影忍者_selected@2x" vcClass:@"VideoViewController"];
    TabBarModel *girlModel = [[TabBarModel alloc] initWithTitle:@"美女" normalImage:@"美女@2x" highLightImage:@"美女_selected@2x" vcClass:@"GirlViewController"];
    //把 TabBarModel 添加到数组中
    [self.tabBarModelArray addObject:jokeModel];
    [self.tabBarModelArray addObject:picModel];
    [self.tabBarModelArray addObject:videoModel];
    [self.tabBarModelArray addObject:girlModel];
}

//2、创建 ContentViewControllers
- (void)createContentViewControllers{
    //注意 一定要先初始化 viewControllers
    NSMutableArray *viewControllers = [NSMutableArray array];
    
    for (int index = 0; index < self.tabBarModelArray.count; index++) {
        TabBarModel *tabBar = [self.tabBarModelArray objectAtIndex:index];
        
        //创建UIViewController,并设置相应的字段 -》创建每个标题的 类的名称
        NSString *className = tabBar.viewControllerName;
        //通过NSClassFromString ，根据 每个标题的 类的名称 -》创建 viewConreoller
        UIViewController *viewConreoller = [[NSClassFromString(className) alloc] init];
        //设置 viewConreoller 的标题
        viewConreoller.title = tabBar.title;
        //注意 基类 是继承于 UITabBarController 的
        //创建 nav -》initWithRootViewController:viewConreoller ->把 viewConreoller 作为nav的根视图(即入口视图)
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewConreoller];
        //设置 nav 的信息
        nav.tabBarItem.image         = [tabBar normalImage];
        nav.tabBarItem.selectedImage = [tabBar seletedImage];
        //把每个 nav 添加到 viewControllers 中
        
        [viewControllers addObject:nav];
    }
    //让UITabBarController的viewControllers 等于 上面得到的viewControllers 
    self.viewControllers = viewControllers;
}


- (void)viewDidLoad {
    [super viewDidLoad];
}

@end
